import { Reducer, Effect } from 'umi';

export interface UserModelState {
  worksData: any;
}

export interface UserModelType {
  namespace: 'addworks';
  state: UserModelState;
  effects: {
    fetch: Effect;
  };
  reducers: {
    save: Reducer<UserModelState>;
  };
}

const UserModel: UserModelType = {
  namespace: 'addworks',

  state: {
    worksData: {},
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = payload;
      yield put({
        type: 'save',
        payload: response ?? {},
      });
    },
  },
  reducers: {
    save(state, action) {
      let worksData = Object.keys(action?.payload)?.length ? { ...state?.worksData, ...action.payload } : {};
      return {
        ...state,
        worksData,
      };
    },
  },
};

export default UserModel;
